$(function () {
    findByItem(1);
})
var layer = null;
layui.use('layer', function(){
     layer = layui.layer;
});

/**
 * 分页查询
 */
function findByItem(pageNO) {
    $.post("/Goods/findByItem",$("#GoodData").serialize()+"&pageNO="+pageNO,function (pageInfo) {
        var str = "";
        for(var i=0;i<pageInfo.list.length;i++){
            var obj = pageInfo.list[i];
            str += `<tr><td>${obj.name}</td><td>${obj.type}</td>
            <td>${obj.factory}</td><td>${obj.stockNum}</td><td>
            <a href="javascript:productInfo(${obj.id})" class="layui-btn layui-btn-sm">
            商品详细信息
            </a>
            <a href="javascript:findById(${obj.id})" class="layui-btn layui-btn-sm" >
            修改销售价
            </a></td></tr>`
        }
        $("#GoodsData").html(str);

        if(pageNO == 1){
            layui.use('laypage', function(){
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'cutPageDiv', //注意，这里的 test1 是 ID，不用加 # 号
                    count: pageInfo.total, //数据总数，从服务端得到
                    limit:pageInfo.pageSize,
                    jump:function (obj,first) {
                        if(!first){
                            findByItem(obj.curr)
                        }
                    }
                })
            })
        };
    })
}

function productInfo(goodsId) {

    $.post("/Goods/findById","goodsId="+goodsId,function (goods) {
        $("#goodsId").html(goods.id);
        $("#goodsName").html(goods.name);
        $("#goodsFactory").html(goods.factory);
        $("#goodsType").html(goods.type);
        $("#purchaseNum").html(goods.stockNum);
        $("#purchasePrice").html(goods.purchasePrice);
        $("#salePrice").html(goods.salePrice);
        $("#imgSpan").attr("src","/html/img/"+goods.image);
    })
    layer.open({
        title:"商品详细信息",//标题
        type:1,//弹出层的类别，1表示页面层
        shadeClose:true,//点击遮罩层关闭
        skin:'layui-layer-rim',//加上边框
        area:['600px','550px'],//宽高
        anim:4,
        content:$("#productInfoDiv")
    });
}

function findById(goodsId) {
    $.post("/Goods/findById","goodsId="+goodsId,function (goodsObj) {
        $("#c").html(goodsObj.id);
        $("#a").html(goodsObj.name);
        $("#b").html(goodsObj.factory);
        $("#d").html(goodsObj.type);
        $("#e").html(goodsObj.stockNum);
        $("#f").html(goodsObj.purchasePrice);
        $("#g").val(goodsObj.salePrice);
    })
    layer.open({
        title:"修改销售价格",//标题
        type:1,//弹出层的类别，1表示页面层
        shadeClose:true,//点击遮罩层关闭
        skin:'layui-layer-rim',//加上边框
        area:['800px','400px'],//宽高
        anim:4,
        content:$("#updatePriceDiv")
    });
}

function updatePrice() {
    $.post("/Goods/updatePrice","goodsId="+$("#c").html()+"&"+$("#updatePriceId").serialize(),function (info){
        if(info == "ok"){
            layer.closeAll();
           findByItem(1);
        }
    })
}



